#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdbool.h>
#include "netconfig.h"

#define CONF_FILE "/etc/network/interfaces"
#define TEMP_FILE "/home/root/interfaces.tmp"

bool get_udhcpc_on(void)
{
    FILE *src;
    char line[256];
    bool found = false;
    
    src = fopen(CONF_FILE, "r");
    if (!src) {
        perror("Unable to open source file");
        return 1;
    }
    
    while (fgets(line, sizeof(line), src)) {
        if (strstr(line, "iface eth0 inet dhcp")) {
            found = true;
			break;
        } 
    }
 
    fclose(src);
    
    return found;
}

int set_udhcpc_ip(unsigned char *mac)
{
    FILE *src, *dst;
    char line[256];
    int found = 0;
    
    src = fopen(CONF_FILE, "r");
    if (!src) {
        perror("Unable to open source file");
        return 1;
    }
    
    dst = fopen(TEMP_FILE, "w");
    if (!dst) {
        perror("Unable to create temporary file");
        fclose(src);
        return 1;
    }
    
    while (fgets(line, sizeof(line), src)) {
        if (strstr(line, "iface eth0 inet")) {
            found = 1;
            fprintf(dst, "iface eth0 inet dhcp\n");
            fprintf(dst, "	pre-up ip link set dev eth0 mtu 9000\n");
            fprintf(dst, "	pre-up ip link set dev eth0 address %02X:%02X:%02X:%02X:%02X:%02X\n",
                mac[0],
                mac[1],
                mac[2],
                mac[3],
                mac[4],
                mac[5]
            );
            fprintf(dst, "	udhcpc_opts -t 10 -O mtu\n");
            while (fgets(line, sizeof(line), src)) {
                if (line[0] == '\n' || !strstr(line, "	")) {
                    fputs(line, dst);
                    break;
                }
            }
        } else {
            fputs(line, dst);
        }
    }
 
    fclose(src);
    fclose(dst);
    
    if (!found) {
        printf("Configuration for eth0 not found\n");
        remove(TEMP_FILE);
        return 1;
    }
    
    if (rename(TEMP_FILE, CONF_FILE) != 0) {
        perror("Cannot replace the original file");
        return 1;
    }
    
    return 0;
}

int set_static_ip(struct in_addr ip, struct in_addr netmask, struct in_addr gateway, unsigned char *mac)
{
    FILE *src, *dst;
    char line[256];
	struct in_addr broadcast;
    int found = 0;
    
    src = fopen(CONF_FILE, "r");
    if (!src) {
        perror("Unable to open source file");
        return 1;
    }
    
    dst = fopen(TEMP_FILE, "w");
    if (!dst) {
        perror("Unable to create temporary file");
        fclose(src);
        return 1;
    }

	broadcast.s_addr = (ip.s_addr&netmask.s_addr)|(~netmask.s_addr);
    
    while (fgets(line, sizeof(line), src)) {
        if (strstr(line, "iface eth0 inet")) {
            found = 1;
            fprintf(dst, "iface eth0 inet static\n");
            fprintf(dst, "	pre-up ip link set dev eth0 address %02X:%02X:%02X:%02X:%02X:%02X\n",
                mac[0],
                mac[1],
                mac[2],
                mac[3],
                mac[4],
                mac[5]
            );
            fprintf(dst, "	address %s\n", inet_ntoa(ip));
            fprintf(dst, "	netmask %s\n", inet_ntoa(netmask));
            fprintf(dst, "	gateway %s\n", inet_ntoa(gateway));
            fprintf(dst, "	broadcast %s\n", inet_ntoa(broadcast));
			fprintf(dst, "	mtu 9000\n");
            while (fgets(line, sizeof(line), src)) {
                if (line[0] == '\n' || !strstr(line, "	")) {
                    fputs(line, dst);
                    break;
                }
            }
        } else {
            fputs(line, dst);
        }
    }
 
    fclose(src);
    fclose(dst);
    
    if (!found) {
        printf("Configuration for eth0 not found\n");
        remove(TEMP_FILE);
        return 1;
    }
    
    if (rename(TEMP_FILE, CONF_FILE) != 0) {
        perror("Cannot replace the original file");
        return 1;
    }
    
    return 0;
}

int get_config_mac(unsigned char *mac)
{
    char *start;
    FILE *file = fopen(CONF_FILE, "r");

    if (!file) {
        perror("Unable to open file");
        return 1;
    }

    char line[256];
    char mac_str[18] = {0};

    while (fgets(line, sizeof(line), file)) {
        start = strstr(line, "eth0 address");
        if (start) {

            while (*start) {
                if (isxdigit(*start) && isxdigit(*(start+1)) && *(start+2) == ':' ) {
                    break;
                }
                start++;
            }

            if (start) {
                sscanf(start, "%17s", mac_str);
                break;
            }
        }
    }
    fclose(file);

    if (mac_str[0] == '\0') {
        printf("mac address not found\n");
        return 1;
    }

    if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
               &mac[0], &mac[1], &mac[2],
               &mac[3], &mac[4], &mac[5]) != 6) {
        printf("mac address format error\n");
        return 1;
    }

    return 0;
}
